package qeb.handheldmedical.exam;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;

import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.ExamScoresItemAdapter;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.download.DownloadActivity;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.User;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年8月31日 下午5:14:18
 * @declaration 成绩列表
 */
public class ExamScoresList extends Activity {
	User usr = new User();
	private SlideListView slvScoreses = null;
	private String examID = "", examType = "", examTitle = "";
	private TextView tv_top_title;
	Button btn_title_back, btn_title_right, btn_examStart;
	private JSONArray examScoresJsonArray = null;
	private ExamScoresItemAdapter adapter;
	private MultiDialog mDialog;

	private final static int EXAM_RESULT_CODE = 1;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		usr = (User) getIntent().getSerializableExtra("userinfo");
		examID = (String) getIntent().getSerializableExtra("examID");
		examType = (String) getIntent().getSerializableExtra("examType");
		examTitle = (String) getIntent().getSerializableExtra("examTitle");

		setContentView(R.layout.exam_scores);
		slvScoreses = (SlideListView) findViewById(R.id.slvScoreses);
		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText(examTitle);
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);

		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setOnClickListener(MyOnClickListener);
		btn_examStart = (Button) findViewById(R.id.btn_examStart);
		btn_examStart.setOnClickListener(MyOnClickListener);

		getData();
		dataBind();

	}

	private void getData() {
		// TODO Auto-generated method stub
		if (usr.BQID != null) {
			Map<String, String> map = new HashMap<String, String>();
			map = new HashMap<String, String>();
			map.put("userid", usr.GH);
			map.put("examid", examID);
			examScoresJsonArray = WSHelper.getWebserviceFun("GetExamResult",
					map);
		}
	}

	/**
	 * 绑定数据
	 */
	private void dataBind() {
		if (examScoresJsonArray != null && examScoresJsonArray.length() > 0) {

			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}

			adapter = new ExamScoresItemAdapter(examScoresJsonArray.length(),
					ExamScoresList.this, examScoresJsonArray);

			slvScoreses.setAdapter(adapter);
			slvScoreses.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					mDialog = new MultiDialog(ExamScoresList.this,
							MultiDialog.DialogType.ALERT);
					mDialog.setMessage("此试卷未开放！");
					mDialog.show();
				}
			});
		}
	}

	private OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_title_back:// 返回按钮
				ExamScoresList.this.finish();
				break;
			case R.id.btn_title_right:// 刷新按钮
				getData();
				dataBind();
				break;
			case R.id.btn_examStart:// 开始考试
				if (null != examScoresJsonArray
						&& examScoresJsonArray.length() >= 3) {
					mDialog = new MultiDialog(ExamScoresList.this,
							MultiDialog.DialogType.ALERT);
					mDialog.setTitle("系统提醒");
					mDialog.setMessage("此试卷您已经超过3次作答！");
					mDialog.setDialogKnowListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							mDialog.cancel();
						}
					});
					mDialog.show();
				} else {

					mDialog = new MultiDialog(ExamScoresList.this,
							MultiDialog.DialogType.CONFIRM);
					mDialog.setTitle("系统提醒");
					mDialog.setMessage("您确定要进入【" + examTitle + "】在线作答！");
					mDialog.setDialogGoListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							mDialog.cancel();

							Intent intent = new Intent();
							intent.setClass(ExamScoresList.this,
									Examination.class);
							Bundle bundle = new Bundle();
							intent.putExtra("userinfo", usr);
							intent.putExtra("examID", examID);
							intent.putExtra("examType", examType);
							intent.putExtra("examTitle", examTitle);
							intent.putExtras(bundle);
							startActivityForResult(intent, EXAM_RESULT_CODE);

						}
					});
					mDialog.show();

				}
				break;
			}
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case EXAM_RESULT_CODE:
			if (resultCode == 1) {
				getData();
				dataBind();
			}
			break;
		}
	}

}
